
import java.sql.*;

public class Hellow {

    public static void main(String[] args) {
        Connection root =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="select *from user";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            //5.执行sql preparedStatement.executeQuery()
            resultSet = preparedStatement.executeQuery();//ResultSet是结果集
            //6.遍历结果
            while (resultSet.next()) {
                //列
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"___"+name+"___"+pass);
            }
/*
            //7关闭资源
            resultSet.close();
            preparedStatement.close();
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
               if (preparedStatement!=null)
                preparedStatement.close();
               if (root!=null)
                root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
